package sz.szsmk.citizencard.util.zxyh;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.Signature;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;

import java.io.File;


/**
 * Created by wutw on 2018/1/4 0004.
 */
public class DeviceUtils {
    /**
     * @return 当前应用版本号
     */
    public static int getAppVersionCode(Context context) {
        int versionCode = 1;
        try {
            versionCode = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            versionCode = 1;

                e.printStackTrace();

        }
        return versionCode;
    }

    /**
     * @return 当前应用版本名
     */
    public static String getAppVersionName(Context context) {
        String versionName = "";
        try {
            versionName = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;
        } catch (PackageManager.NameNotFoundException e) {
            versionName = "";

                e.printStackTrace();

        }
        return versionName;
    }

    //手机设备id
    @SuppressLint("MissingPermission")
    public static String getDeviceId(Context context) {
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        return !TextUtils.isEmpty(tm.getDeviceId()) ? tm.getDeviceId() : "";
    }

    public static void getSignature(Context con){
        try {
            String pkgname = con.getPackageName();
            PackageManager manager = con.getPackageManager();
            /** 通过包管理器获得指定包名包含签名的包信息 **/
            PackageInfo packageInfo = manager.getPackageInfo(pkgname, PackageManager.GET_SIGNATURES);
            /******* 通过返回的包信息获得签名数组 *******/
            Signature[] signatures = packageInfo.signatures;
            /************** 得到应用签名 **************/
            String signature = signatures[0].toCharsString();
            Log.d("signature","---->"+signature);
            /************** 请将signature的值即应用签名 反馈给我 谢谢 **************/


        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 通过Uri找到File
     *
     * @param context context
     * @param uri uri
     * @return File
     */
    public static File uri2File(Activity context, Uri uri) {
        File file;
        String[] project = { MediaStore.Images.Media.DATA };
        Cursor actualImageCursor = context.getContentResolver()
                .query(uri, project, null, null, null);
        if (actualImageCursor != null) {
            int actual_image_column_index = actualImageCursor.getColumnIndexOrThrow(
                    MediaStore.Images.Media.DATA);
            actualImageCursor.moveToFirst();
            String img_path = actualImageCursor.getString(actual_image_column_index);
            file = new File(img_path);
        } else {
            file = new File(uri.getPath());
        }
        if (actualImageCursor != null) actualImageCursor.close();
        return file;
    }
}
